var file_number = 1;

function reloadRegions(country) {
	$('#RegionID').children().remove();
	$.ajax({
		type: 'POST',
		url: HTTP_HOST + '/site-manager/object/reload-regions',
		data: {id:country},
		success: function(data, textStatus){showRegions(data);},
		dataType: 'json'
	});
}

function showRegions(data) {
	for (i in data.RegionID) $('#RegionID').append($('<option />').val(i).html(data.RegionID[i])); 
}

function reloadCities(region) {
	$('#CityID').children().remove();
	$.ajax({
		type: 'POST',
		url: HTTP_HOST + '/site-manager/object/reload-cities',
		data: {id:region},
		success: function(data, textStatus){showCities(data);},
		dataType: 'json'
	});
}

function showCities(data) {
	for (i in data.cities) $('#CityID').append($('<option />').val(i).html(data.cities[i])); 
}

function reloadLocations(region) {
	$('#LocationID').children().remove();
	$.ajax({
		type: 'POST',
		url: HTTP_HOST + '/site-manager/object/reload-locations',
		data: {id:region},
		success: function(data, textStatus){showLocations(data);},
		dataType: 'json'
	});
}

function showLocations(data) {
	for (i in data.locations) $('#LocationID').append($('<option />').val(i).html(data.locations[i])); 
}

function processSaveObject(data) {
	if(data.error == 2){
		window.location.reload();
	}else {
		if (data.error != 0) {
			alert(data.errorStr);
			return;
		}
		window.location.href = data.redirect;
	}
}

function showLoader(){
	$("#loader_bg").show();
	$("#loader_div").show();
}

$(function(){
	
	
	function resetPopUp(){
		$("#data_popup_main_wrap").html('');
		$("#data_popup_title").html('');
	}
	
	$(".del_o").click(function(){
		var id = $(this).attr('id');
		var name = $(this).attr('rel');
		if(confirm('Вы действительно хотите удалить объект '+name+'?')){
			showLoader()
			$.ajax({
				type: 'POST',
				url: HTTP_HOST + '/site-manager/object/delete',
				data: {id:id},
				success: function(data, textStatus){
					processSaveObject(data);
				},
				dataType: 'json'
			});
		}
	})

	$(".state1").click(function(){
		showLoader();
		_ = $(this).attr('id');
		arID = _.split('-');
		var id = arID[1];
		$.ajax({
			type: 'POST',
			url: HTTP_HOST + '/site-manager/object/state-changer',
			data: {id:id},
			success: function(data, textStatus){processSaveObject(data);},
			dataType: 'json'
		});		
	})
	
	$(".enable_all").click(function(){
		flag = $(this).attr('checked');
		if(!flag){
			$(".object_ch").attr('checked', false);
			$(".enable_all").attr('checked', false);
		}else{
			$(".object_ch").attr('checked', 'checked');
			$(".enable_all").attr('checked', 'checked');
		}
	})	
	
	$("div#new_file_div").click(function(){
		++file_number;
		$("tbody#files_table").append("<tr id='objectfield_"+file_number+"'><td><input type='file' name='objectFile[]'/></td><td><input type='radio' name='main' value='objectfile_"+file_number+"'></td></tr>");
	})

	$(".delete_object_image").click(function(){
		id = $(this).attr('id');
		name = $(this).attr('rel');
		parent=$("input[name=ID]").val()
		if(confirm('Вы действительно хотите удалить картинку '+name+'?')){
			$.ajax({
				type: 'POST',
				url: HTTP_HOST + '/site-manager/object/delete-object-image',
				data: {id:id, parent:parent},
				success: function(data, textStatus){
					processSaveObject(data);
				},
				dataType: 'json'
			});
		}
	})
	
	$(".main_page_image").click(function(){
		id = $(this).attr('id');
		name = $(this).attr('rel');
		parent=$("input[name=ID]").val();
		$.ajax({
			type: 'POST',
			url: HTTP_HOST + '/site-manager/object/set-main-page',
			data: {id:id, parent:parent},
			success: function(data, textStatus){
				processSaveObject(data);
			},
			dataType: 'json'
		});
	})
	
	$(".spec_offer_btn").click(function(){
		_ = $(this).attr('id');
		arID = _.split('_');
		var id = arID[1];
		$.ajax({
			type: 'POST',
			url: HTTP_HOST + '/site-manager/object/show-object-special-offers',
			data: {id:id},
			success: function(data){
				$("#data_popup_title").html('Выберите акцию');
				$("#data_popup_main_wrap").html(data.data);
				$("#loader_bg").show();
				$("#data_popup_bg").show();
				$("#save_special_offers_for_object").click(function(){
					var arrCheckbox = [];
					$('.sp_of:checked').each(function(index) {
					    arrCheckbox.push($(this).attr('name'));
					});
					$.ajax({
						type: 'POST',
						url: HTTP_HOST + '/site-manager/object/set-object-special-offers',
						data: {id:id, arrCheckbox:arrCheckbox},
						success: function(data, textStatus){
							resetPopUp();
							processSaveObject(data);
						},
						dataType: 'json'
					});
				})				
			},
			dataType: 'json'
		});
	})
	
	$(".same_obects").click(function(){
		_ = $(this).attr('id');
		arID = _.split('_');
		var id = arID[1];
		$.ajax({
			type: 'POST',
			url: HTTP_HOST + '/site-manager/object/show-same-objects',
			data: {id:id},
			success: function(data){
				$("#data_popup_title").html('Введите код сопутствующих объектов');
				$("#data_popup_main_wrap").html(data.data);
				$("#loader_bg").show();
				$("#data_popup_bg").show();
				$("#save_same_objects_button").click(function(){
					var sameArr = $("#same_objects_field").val();
					$.ajax({
						type: 'POST',
						url: HTTP_HOST + '/site-manager/object/set-same-objects',
						data: {id:id, sameArr:sameArr},
						success: function(data, textStatus){
							resetPopUp();
							processSaveObject(data);
						},
						dataType: 'json'
					});
				})
			},
			dataType: 'json'
		});
	})
	
	$("#group_delete").click(function(){
		var arrCheckbox = [];
		$('.object_ch:checked').each(function(index) {
		    arrCheckbox.push($(this).attr('name'));
		});
		if (arrCheckbox.length != 0){
			if(confirm('Вы действительнно хотите удалить выбраные объекты?')){
				showLoader();
				$.ajax({
					type: 'POST',
					url: HTTP_HOST + '/site-manager/object/grouped-delete',
					data: {'arDelete':arrCheckbox},
					success: function(data, textStatus){processSaveObject(data);},
					dataType: 'json'
				});
			}
		}
	})
	
})
